package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Department;
import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.EmployeeQueryObject;
import cn.wolfcode.wms.service.DepartmentService;
import cn.wolfcode.wms.service.EmployeeService;
import cn.wolfcode.wms.service.RoleService;
import cn.wolfcode.wms.service.SystemMenuService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;


    @RequiredPermission("员工查询")
    @RequestMapping("/list")
    public String list(Map map, EmployeeQueryObject qo) {
        List<Department> dept = departmentService.selectAll();

        map.put("dept", dept);
        map.put("qo", qo);
        PageResult result = employeeService.query(qo);
        System.out.println(result.getTotalCount()+"==================");
        map.put("result", result);
        return "employee/list";
    }

    @RequiredPermission("员工编辑")
    @RequestMapping("/input")
    public String input(Map map, Long id) {

        List<Role> roles = roleService.selectAll();
        map.put("roles", roles);
        List<Department> dept = departmentService.selectAll();
        map.put("dept", dept);
        if (id != null && id != 0) {
            Employee emp = employeeService.getById(id);
            map.put("emp", emp);
        }
        return "/employee/input";
    }

    @RequiredPermission("员工删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        try {

            employeeService.deleteById(id);

        } catch (Exception e) {
            return JSONResult.mark(e.getLocalizedMessage());
        }
        return JSONResult.success("删除成功");
    }

    @RequiredPermission("员工新增/修改")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Employee emp, Long[] ids) {
        try {
        employeeService.updateOrSave(emp, ids);
        }catch (Exception e){
            return JSONResult.mark(e.getLocalizedMessage());
        }
        return JSONResult.success("修改成功");
    }
    @RequiredPermission("批量删除")
    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteall(Long[] ids) {
        try {
            System.out.println("nmsl");
            System.out.println(ids);
            employeeService.deleteAll(ids);
            System.out.println("nmsl");
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getLocalizedMessage());
        }
        return JSONResult.success("删除成功");
    }
    @RequiredPermission("NM$L")
    public void nmsl(){}
}
